# coding=utf-8
import web


urls = (
    '/', 'index',
    '/upper/(.*)', 'upper',
    '/lower/(.*)', 'lower',
    '/upper_html/(.*)', 'upper_html',
    '/showimg', 'showimg',
    '/dbquery', 'dbquery',
    '/(js|css|images)/(.*)', 'static',
)

app = web.application(urls, globals())

render = web.template.render('templates/')


class index:
    def GET(self):
        print('hello web.py')
        return 'hello web.py'


class upper:
    def GET(self, text):
        print('input:' + text)
        return text.upper()


class lower:
    def GET(self, text):
        print('input:' + text)
        return text.lower()


class upper_html:
    def GET(self, text):
        print('input:' + text)
        return render.index(content=text.upper())


class dbquery:
    def GET(self):
        db = web.database(dbn='mysql', host='127.0.0.1',
                          port=3306, user='root', pw='root', db='test', charset='utf8')
        name = 'hello web.py'
        blogs = db.query('select * from article_article')
        return render.dbquery(name=name, blogs=blogs)


class static:
    def GET(self, media, file):
        # 以二进制方式打开文件
        with open(media + '/' + file, 'rb') as f:
            x = f.read()
        return x


if __name__ == '__main__':
    app.run()
